from typing import Optional
from pydantic import BaseModel, Field, field_validator
from shared.config import settings


class ConversionOptions(BaseModel):
    """转换选项"""
    theme: str = Field(default=settings.default_theme, description="演示文稿主题")
    output_format: str = Field(default="pptx", description="输出格式")
    image_width_ratio: float = Field(default=settings.default_image_width_ratio, description="图片宽度比例")
    
    @field_validator('theme')
    @classmethod
    def validate_theme(cls, v):
        if v not in settings.supported_themes:
            raise ValueError(f"Unsupported theme: {v}. Supported: {', '.join(settings.supported_themes)}")
        return v
    
    @field_validator('output_format')
    @classmethod
    def validate_output_format(cls, v):
        if v != "pptx":
            raise ValueError("Only 'pptx' format is supported")
        return v
    
    @field_validator('image_width_ratio')
    @classmethod
    def validate_image_width_ratio(cls, v):
        if v <= 0 or v > 1:
            raise ValueError("Image width ratio must be between 0 and 1")
        return v


class ConvertRequest(BaseModel):
    """转换请求"""
    markdown_content: str = Field(..., description="Markdown 内容")
    options: ConversionOptions = Field(default_factory=ConversionOptions, description="转换选项")
    output_filename: Optional[str] = Field(None, description="输出文件名")
    
    @field_validator('markdown_content')
    @classmethod
    def validate_markdown_content(cls, v):
        if not v or not v.strip():
            raise ValueError("Markdown content cannot be empty")
        if len(v) > 10 * 1024 * 1024:  # 10MB
            raise ValueError("Markdown content too large (max 10MB)")
        return v
    
    @field_validator('output_filename')
    @classmethod
    def validate_output_filename(cls, v):
        if v is not None:
            if not v.endswith('.pptx'):
                raise ValueError("Output filename must have .pptx extension")
            if len(v) > 255:
                raise ValueError("Filename too long (max 255 characters)")
        return v


class FileConvertRequest(BaseModel):
    """文件转换请求"""
    file_path: str = Field(..., description="Markdown 文件路径")
    options: ConversionOptions = Field(default_factory=ConversionOptions, description="转换选项")
    output_filename: Optional[str] = Field(None, description="输出文件名")


class BatchConvertRequest(BaseModel):
    """批量转换请求"""
    files: list[ConvertRequest] = Field(..., description="要转换的文件列表")
    
    @field_validator('files')
    @classmethod
    def validate_files(cls, v):
        if not v:
            raise ValueError("Files list cannot be empty")
        if len(v) > 10:  # 限制批量转换数量
            raise ValueError("Too many files (max 10)")
        return v